package com.msb.service.service;

import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.dto.PassengerUser;
import com.msb.service.mapper.PassengerUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: jiudianliu
 * @Date: 2023/5/19 - 05 - 19 - 16:06
 * @Description: com.msb.service.service
 * @version: 1.0
 */
@Service
public class UserService {

    @Autowired
    private PassengerUserMapper passengerUserMapper ;

    public ResponseResult loginOrRegister(String passengerPhone){

        //System.out.println("调到,手机号：" + passengerPhone);

        //根据手机号查询用户信息
        //将手机号存入数据库
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);

        //检查功能是否能跑通
        //System.out.println(passengerUsers.size() == 0 ? "无记录" : passengerUsers.get(0).getPassengerName());

        //判断用户信息是否存在
        if (passengerUsers.size() == 0){
            //如果不存在就插入此条信息
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerName("");
            passengerUser.setPassengerGender( (byte) 0 );
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setState((byte) 0);
            LocalDateTime localDateTime = LocalDateTime.now();
            passengerUser.setGmtCreate(localDateTime);
            passengerUser.setGmtModified(localDateTime);
            passengerUserMapper.insert(passengerUser);
        }

        return ResponseResult.success();
    }

    /**
     * 根据手机号查询用户信息
     * @param passengerPhone
     * @return
     */
    public ResponseResult getUserByPhone(String passengerPhone){
        //根据手机号查询
        Map<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> passengerUsers = passengerUserMapper.selectByMap(map);
        if (passengerUsers.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS.getCode(), CommonStatusEnum.USER_NOT_EXISTS.getValue());
        }else{
            PassengerUser passengerUser = passengerUsers.get(0) ;
            return ResponseResult.success(passengerUser) ;
        }
    }
}
